package com.walkline.screen;

import localization.RedFlowerResource;
import net.rim.device.api.collection.util.SortedReadableList;
import net.rim.device.api.i18n.ResourceBundle;
import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.KeywordFilterField;
import net.rim.device.api.ui.component.KeywordProvider;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.util.Comparator;
import net.rim.device.api.util.StringUtilities;

import com.walkline.app.RedFlowerAppConfig;
import com.walkline.util.Functions;

public class VendorsSearchScreen extends MainScreen implements RedFlowerResource
{
	private static ResourceBundle _resources = ResourceBundle.getBundle(BUNDLE_ID, BUNDLE_NAME);
	
    private KeywordFilterField _keywordFilterField;    
    private VendorList _vendorList;
    
    public VendorsSearchScreen()
    {
    	super(NO_VERTICAL_SCROLL | NO_VERTICAL_SCROLLBAR | NO_SYSTEM_MENU_ITEMS);
        _vendorList = new VendorList();
        
		for(int i=0; i<=RedFlowerAppConfig.VendorID.length-1; i++)
		{
			_vendorList.addElement(new Vendor(Functions.formatID(RedFlowerAppConfig.VendorID[i]) + ": " + RedFlowerAppConfig.VendorName[i], i));
		}

        _keywordFilterField = new KeywordFilterField();
        _keywordFilterField.setLabel(getResString(VENDOR_SEARCH_TIPS));
        _keywordFilterField.setSourceList(_vendorList, _vendorList);
        _keywordFilterField.setRowHeight(Font.getDefault().getHeight()+RedFlowerAppConfig.ListRowHeightOffset);
        _keywordFilterField.setCallback(new ListFieldCallback()
        {
			public int indexOfList(ListField listField, String prefix, int start) {return 0;}
			public int getPreferredWidth(ListField listField) {return 0;}
			public Object get(ListField listField, int index) {return null;}

			public void drawListRow(ListField listField, Graphics g, int index,	int y, int w)
			{
				String text=_keywordFilterField.getElementAt(index).toString();
				g.drawText(text, 0, y+(listField.getRowHeight()-Font.getDefault().getHeight())/2,ButtonField.ELLIPSIS,w);
			}
		});

        setTitle(getResString(VENDOR_INFORMATION_VIEW_ALL_VENDORS) + " (" + RedFlowerAppConfig.VendorID.length + getResString(VENDOR_SEARCH_ITEMUNIT) + ")");
        VerticalFieldManager vfm1=new VerticalFieldManager();
        VerticalFieldManager vfm2=new VerticalFieldManager(VERTICAL_SCROLL | VERTICAL_SCROLLBAR);
        vfm1.setBackground(RedFlowerAppConfig.bgColor_black);
        vfm1.add(_keywordFilterField.getKeywordField());
        vfm2.add(_keywordFilterField);

        add(vfm1);
        add(vfm2);
    }

	private String getResString(int key) {return _resources.getString(key);}

    protected boolean keyChar(char character, int status, int time)
    {
    	switch (character)
    	{
			case Characters.ENTER:
		    	Field focus = UiApplication.getUiApplication().getActiveScreen().getLeafFieldWithFocus();

		    	if (focus == _keywordFilterField)
		    	{
		    		Vendor vendor = (Vendor)_keywordFilterField.getSelectedElement();
		    		int index = vendor.getIndex();

		    		if (index >= 0)
		    		{
		       			VendorNameScreen vns = new VendorNameScreen(RedFlowerAppConfig.VendorName[index], RedFlowerAppConfig.VendorID[index]);
		   				UiApplication.getUiApplication().pushScreen(vns);
		    		}
		    		return true;
		    	}
		}

		return super.keyChar(character, status, time);
	}

    protected boolean navigationClick( int status, int time ) 
    {
    	Field focus = UiApplication.getUiApplication().getActiveScreen().getLeafFieldWithFocus();

    	if (focus == _keywordFilterField)
    	{
    		Vendor vendor = (Vendor)_keywordFilterField.getSelectedElement();
    		int index = vendor.getIndex();

    		if (index >= 0)
    		{
       			VendorNameScreen vns = new VendorNameScreen(RedFlowerAppConfig.VendorName[index], RedFlowerAppConfig.VendorID[index]);
   				UiApplication.getUiApplication().pushScreen(vns);
    		}
    		return true;
    	}

        return super.navigationClick(status, time);    
    }
}

class VendorList extends SortedReadableList implements KeywordProvider
{
	public VendorList() {super(new VendorListComparator());} 

	void addElement(Object element) {doAdd(element);}

	public String[] getKeywords(Object element)
	{
		if(element instanceof Vendor)
		{
			return StringUtilities.stringToWords(element.toString());
		}

		return null;
	}

	final static class VendorListComparator implements Comparator
	{
		public int compare(Object o1, Object o2)
		{
			if (o1 == null || o2 == null)
			{
				throw new IllegalArgumentException("Cannot compare null vendors");
			}

			return o1.toString().compareTo(o2.toString());
		}
	}
}

class Vendor
{
	private String _vendorName;
	private int _index;

	public Vendor(String vendorName, int index)
	{
		_vendorName = vendorName;        
		_index = index;
	}

	public String toString() {return _vendorName;}
	public int getIndex() {return _index;}
}